document.write('<script language=javascript src="../js/jquery-3.5.1.min.js"></script>');
//document.write('<script language=javascript src="js/jquery-3.6.0.min.js"></script>');
document.write('<link rel="stylesheet" href="../css/register.css">');
/*
var xhr;
function checkUsername(){
    var username = document.getElementById("username").value;//拿到用户输入的值
    console.log(username);
    xhr = new XMLHttpRequest();
    xhr.onreadystatechange = fun1;//状态改变时调用函数
    //向服务器发送请求
    xhr.open("GET","usernameExit?username="+username,true);
    xhr.send(null);
}
//回调函数
function fun1(){
    if(xhr.readyState===4){
        if(xhr.status===200){
            //成功
            var tips=document.getElementById("usernameTips");
            var responseInfo = xhr.responseText;
            console.log(responseInfo);
            if(responseInfo==="Exist"){
                //用户名不可用
                tips.className="invalid";
                tips.innerText="Invalid";
            }else if(responseInfo==="Not Exist"){
                //用户名可用
                tips.className="available";
                tips.innerText="Available";
            }
        }
    }
}
*/
$(function (){
    $('#username').on('blur',function (){
        // $.ajax({
        //     type    : "GET",
        //     url     : "usernameExist?username="+this.value,
        //     success : function (data){
        //         if(data.code===2){
        //             $('#usernameTips').attr("class","invalid").text("输入不可为空");//样式
        //         }else if(data.code===0){
        //             $('#usernameTips').attr("class","invalid").text("用户名不可用");//样式
        //             //$('#usernameTips').text = "Invalid";//内容
        //         }else if(data.code===1){
        //             $('#usernameTips').attr("class","available").text("用户名可用");
        //         }
        //     }
        // });
        var url
        if(!document.getElementById("listOption").checked&&!document.getElementById("bannerOption").checked){
            console.log("两个都未选中");
            url='/account/newAccount?listOption=false&bannerOption=false&isclick=0';
        }else if(document.getElementById("listOption").checked&&!document.getElementById("bannerOption").checked){
            console.log("第二个未选中");
            url='/account/newAccount?bannerOption=false&isclick=0';
        }else if(!document.getElementById("listOption").checked&&document.getElementById("bannerOption").checked){
            console.log("第1个未选中");
            url='/account/newAccount?listOption=false&isclick=0';
        }else{
            console.log("两个都选中");
            url='/account/newAccount?isclick=0';
        }
        let datas = $('#register').serialize();
        $.post(url,datas,function (data) {
            if(data.code===0){
                $('#usernameTips').attr("class","invalid").text("输入不可为空");
            }else if (data.code===1){
                $('#usernameTips').attr("class","invalid").text("用户名不可用");
            }else{
                $('#usernameTips').attr("class","available").text("用户名可用");
            }
        });
    });
    $('#password').on('blur',function (){
        if(this.value.length===0||document.getElementById("password").value===null){
            $('#passwordTips').attr("class","invalid").text("输入不可为空");
        }else{
            $('#passwordTips').attr("class","available").text("格式正确");
        }
    });
    $('#repeatedPassword').on('blur',function (){
        if(this.value.length===0||document.getElementById("repeatedPassword").value===null){
            //重复密码为空
            $('#repeatedPasswordTips').attr("class","invalid").text("输入不可为空");
        }else if(document.getElementById("password").value===this.value){
            //重复密码输入正确
            $('#repeatedPasswordTips').attr("class","available").text("输入密码一致");
        }else{
            $('#repeatedPasswordTips').attr("class","invalid").text("输入密码不一致");
        }
    });
    $('#vCode').on('blur',function (e) {
        e.preventDefault();
        var url
        if(!document.getElementById("listOption").checked&&!document.getElementById("bannerOption").checked){
            console.log("两个都未选中");
            url='/account/newAccount?listOption=false&bannerOption=false&isclick=0';
        }else if(document.getElementById("listOption").checked&&!document.getElementById("bannerOption").checked){
            console.log("第二个未选中");
            url='/account/newAccount?bannerOption=false&isclick=0';
        }else if(!document.getElementById("listOption").checked&&document.getElementById("bannerOption").checked){
            console.log("第1个未选中");
            url='/account/newAccount?listOption=false&isclick=0';
        }else{
            console.log("两个都选中");
            url='/account/newAccount?isclick=0';
        }
        let datas = $('#register').serialize();
        $.post(url,datas,function (data) {
            if(document.getElementById("vCode").value===null||document.getElementById("vCode").value.length===0){
                $('#vCodeTips').attr("class","invalid").text("输入不可为空");
            }else if(data.code===3){
                $('#vCodeTips').attr("class","invalid").text("验证码错误");
            }else if(data.code===5){
                $('#vCodeTips').attr("class","available").text("验证码正确");
            }
        });
    });
    $('#newAccount').on('click',function (e) {
        e.preventDefault();
        var url
        if(!document.getElementById("listOption").checked&&!document.getElementById("bannerOption").checked){
            console.log("两个都未选中");
            url='/account/newAccount?listOption=false&bannerOption=false&isclick=1';
        }else if(document.getElementById("listOption").checked&&!document.getElementById("bannerOption").checked){
            console.log("第二个未选中");
            url='/account/newAccount?bannerOption=false&isclick=1';
        }else if(!document.getElementById("listOption").checked&&document.getElementById("bannerOption").checked){
            console.log("第1个未选中");
            url='/account/newAccount?listOption=false&isclick=1';
        }else{
            console.log("两个都选中");
            url='/account/newAccount?isclick=1';
        }
        let datas = $('#register').serialize();
        if(document.getElementById("password").value!==null&&document.getElementById("password").value.length!==0&&
        document.getElementById("password").value===document.getElementById("repeatedPassword").value){
            console.log("格式");
            $.post(url,datas,function (data) {
                if(data.code===2){
                    console.log("456");
                    location.href="/account/signonForm";
                }
            });
        }else{
            window.alert("请按照正确格式填写信息");
        }

    });
});
// $(function (){
//     $('#password').on('blur',function (){
//         if(this.value.length===0){
//             $('#passwordTips').attr("class","invalid").text("输入不可为空");
//         }else{
//             $('#passwordTips').attr("class","available").text("格式正确");
//         }
//     });
// });
// $(function (){
//     $('#repeatedPassword').on('blur',function (){
//         if(this.value.length===0){
//             //重复密码为空
//             $('#repeatedPasswordTips').attr("class","invalid").text("输入不可为空");
//         }else if(this.value==document.getElementById("password").value){
//             //重复密码输入正确
//             $('#repeatedPasswordTips').attr("class","available").text("输入密码一致");
//         }else{
//             $('#repeatedPasswordTips').attr("class","invalid").text("输入密码不一致");
//         }
//     });
// });
// $(function (){
//     $('#vCode').on('blur',function (){
//         $.ajax({
//             type    : "GET",
//             url     : "checkCode?vCode="+this.value,
//             success : function (data){
//                 if(data.code===3){
//                     //验证码框未输入
//                     $('#vCodeTips').attr("class","invalid").text("输入不可为空");
//                 }else if(data.code===4){
//                     $('#vCodeTips').attr("class","available").text("验证码正确");
//                 }else if(data.code===5){
//                     $('#vCodeTips').attr("class","invalid").text("验证码错误");
//                 }
//             }
//         });
//     });
// });
//
// $(function (){
//     $('#newAccount').on('click',function (){
//         var result;
//         var result1;
//         var result2;
//         $.ajax({
//             type    : "GET",
//             url     : "usernameExist?username="+document.getElementById("username").value,
//             success : function (data){
//                 if(data.code===1){
//                     result=1;
//                     if(document.getElementById("password").value.length===0){
//                         $('#newAccountTips').attr("class","invalid").text("请输入正确信息");
//                     }else if(document.getElementById("repeatedPassword").value==document.getElementById("password").value){
//                         //重复密码输入正确
//                         result1=1;
//                     }else{
//                         $('#newAccountTips').attr("class","invalid").text("请输入正确信息");
//                     }
//                     $.ajax({
//                         type    : "GET",
//                         url     : "checkCode?vCode="+document.getElementById("vCode").value,
//                         success : function (data){
//
//                             if(data.code===4){
//                                 result2=1;
//                             }
//                             if(result==1&&result1==1&&result2==1){
//                                 var formElement = document.forms[1];
//                                 formElement.submit();
//                             }else{
//                                 $('#newAccountTips').attr("class","invalid").text("请输入正确信息");
//                             }
//                         }
//                     });
//                 }else{
//                     $('#newAccountTips').attr("class","invalid").text("请输入正确信息");
//                 }
//             }
//         });
//     });
// });

